package com.yanqu.road.server.gameplayer.module.player;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.dafuweng.DFWConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.config.inn.DishesInfo;
import com.yanqu.road.entity.enums.eGoodsType;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.entity.player.UserHistoryAttribute;
import com.yanqu.road.logic.bussiness.player.UserBagBussiness;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.BagPb;
import com.yanqu.road.pb.bag.BagProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.douluodalu.DouLuoDaLuModule;
import com.yanqu.road.server.gameplayer.module.activity.drink.DrinkModule;
import com.yanqu.road.server.gameplayer.module.activity.dunhuangshiku.DunHuangShiKuModule;
import com.yanqu.road.server.gameplayer.module.activity.suitang.SuiTangModule;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.resource.GoodsResourceModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.HolidayActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.able.ActivityAbleMgr;
import com.yanqu.road.server.manger.activity.busyfarming.BusyFarmingMgr;
import com.yanqu.road.server.manger.activity.celebration.CelebrationMgr;
import com.yanqu.road.server.manger.activity.cookboy.CookBoyActivityMgr;
import com.yanqu.road.server.manger.activity.dafuweng.DaFuWengMgr;
import com.yanqu.road.server.manger.activity.douluodalu.DouLuoDaLuMgr;
import com.yanqu.road.server.manger.activity.dunhuangshiku.DunHuangShiKuMgr;
import com.yanqu.road.server.manger.activity.pawnshop.PawnshopMgr;
import com.yanqu.road.server.manger.activity.ppl.PplMgr;
import com.yanqu.road.server.manger.activity.recycle.ActivityItemRecycleMgr;
import com.yanqu.road.server.manger.activity.shuihuyezicard.ShuiHuYeZiCardMgr;
import com.yanqu.road.server.manger.config.DishesMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.system.CommonMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;


public class BagModule extends GeneralModule {

    private Map<Integer, UserBagItem> userBagItemMap;

    private List<Integer> changedBagItemGoodsIdList = new ArrayList<>();

    private AtomicInteger changeCount = new AtomicInteger(0);

    private final int MAX_CHANGE_ITEM_COUNT = 30;

    public BagModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        userBagItemMap = UserBagBussiness.getUserBagItemMap(getUserId());
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return false;
    }

    public void afterLogin() {
        // 热更 如果 赚速  > 0 且背包没有这道具 就加一个
        UserHistoryAttribute userHistoryAttribute = player.getModule(PlayerModule.class).getUserHistoryAttribute();
        BigInteger earnSpeed = userHistoryAttribute.getEarnSpeed();
        if (earnSpeed == null || earnSpeed.compareTo(BigInteger.ZERO) > 0) {
            int goodsId = 119999;
            UserBagItem userBagItem = userBagItemMap.get(goodsId);
            if (userBagItem == null || userBagItem.getNum() == 0) {
                addBagItem(goodsId,1, eLogMoneyType.System);
            }
        }

        // 回收蛐蛐促织帖道具
        recycleItem(110241, new Property(GameConfig.GAME_MONEY_INGOTS, GameConfig.CRICKET_FIGHT_BACK_INVITA_GOLD), MailManager.CRICKET_FIGHT_BACK_REWARD_TITLE, MailManager.CRICKET_FIGHT_BACK_REWARD_CONTENT);


        // 卖药郎联动过期处理
        // 怪化猫积分->降妖积分
        recycleLinkageItem(101826, 101839);
        // 天平
        recycleLinkageItem(123001, 123100);
        // 没有卖药郎才回收
        if (player.getModule(PatronsModule.class).getUserPatrons(102511) == null) {
            // 符咒退魔->莲花塑身
            recycleLinkageItem(109142, 109150);
            // 斩妖除魔->哪吒闹海
            recycleLinkageItem(109143, 109151);
            // 符咒退魔碎片->莲花塑身碎片
            recycleLinkageItem(179142, 179150);
            // 斩妖除魔碎片->哪吒闹海碎片
            recycleLinkageItem(179143, 179151);
        }

        // 清空花石纲铲子
      /*  if (!ActivityMgr.activityInShowTime(HuaShiGangMgr.getActivityInfo())) {
            UserBagItem userBagItem = userBagItemMap.get(eItemId.HUA_SHI_GANG_SHOVEL.getValue());
            if (userBagItem != null && userBagItem.getNum() > 0) {
                player.getModule(CurrencyModule.class).removeCurrency(new Property(userBagItem.getGoodsId(), BigInteger.valueOf(userBagItem.getNum())), eLogMoneyType.HuaShiGang, eLogMoneyType.HuaShiGangChanZiHuiShou);
            }
        }*/
        // 清空投壶道具
       /* if (!ActivityMgr.activityInShowTime(ThrowingPotMgr.getActivityInfo())) {
            UserBagItem userBagItem1 = userBagItemMap.get(eItemId.THROWING_POT_JIAN_DAI.getValue());
            if (userBagItem1 != null && userBagItem1.getNum() > 0) {
                player.getModule(CurrencyModule.class).removeCurrency(new Property(userBagItem1.getGoodsId(), BigInteger.valueOf(userBagItem1.getNum())), eLogMoneyType.ThrowingPot, eLogMoneyType.ThrowingPotItemHuiShou);
            }
            UserBagItem userBagItem2 = userBagItemMap.get(eItemId.THROWING_POT_JIAN_HU.getValue());
            if (userBagItem2 != null && userBagItem2.getNum() > 0) {
                player.getModule(CurrencyModule.class).removeCurrency(new Property(userBagItem2.getGoodsId(), BigInteger.valueOf(userBagItem2.getNum())), eLogMoneyType.ThrowingPot, eLogMoneyType.ThrowingPotItemHuiShou);
            }
            UserBagItem userBagItem3 = userBagItemMap.get(eItemId.THROWING_POT_SCORE.getValue());
            if (userBagItem3 != null && userBagItem3.getNum() > 0) {
                player.getModule(CurrencyModule.class).removeCurrency(new Property(userBagItem3.getGoodsId(), BigInteger.valueOf(userBagItem3.getNum())), eLogMoneyType.ThrowingPot, eLogMoneyType.ThrowingPotItemHuiShou);
            }
        }*/

        //清空瑞兽烟火道具
       /* if (!ActivityMgr.activityInShowTime(YearMasterMgr.getActivityInfo())) {
            UserBagItem userBagItem3 = userBagItemMap.get(eItemId.YEAR_MASTER_FIREWORKS.getValue());
            if (userBagItem3 != null && userBagItem3.getNum() > 0) {
                player.getModule(CurrencyModule.class).removeCurrency(new Property(userBagItem3.getGoodsId(), BigInteger.valueOf(userBagItem3.getNum())), eLogMoneyType.YearMaster, eLogMoneyType.YearMasterItemHuiShou);
            }
        }*/

        // 清空泡泡龙道具
    /*    if (!PplMgr.isActivityInShowTime()) {
            UserBagItem userBagItem1 = userBagItemMap.get(PplMgr.PPL_BOOK_MOUNTAIN_RIVER_ID);
            if (userBagItem1 != null && userBagItem1.getNum() > 0) {
                player.getModule(CurrencyModule.class).removeCurrency(new Property(userBagItem1.getGoodsId(), BigInteger.valueOf(userBagItem1.getNum())), eLogMoneyType.Ppl, eLogMoneyType.PplAutoEmptyGoods);
            }
            UserBagItem userBagItem2 = userBagItemMap.get(PplMgr.PPL_BOOK_FISHING_SONG_ID);
            if (userBagItem2 != null && userBagItem2.getNum() > 0) {
                player.getModule(CurrencyModule.class).removeCurrency(new Property(userBagItem2.getGoodsId(), BigInteger.valueOf(userBagItem2.getNum())), eLogMoneyType.Ppl, eLogMoneyType.PplAutoEmptyGoods);
            }
            UserBagItem userBagItem4 = userBagItemMap.get(eItemId.PPL_COMMON_BOX.getValue());
            if (userBagItem4 != null && userBagItem4.getNum() > 0) {
                player.getModule(CurrencyModule.class).removeCurrency(new Property(userBagItem4.getGoodsId(), BigInteger.valueOf(userBagItem4.getNum())), eLogMoneyType.Ppl, eLogMoneyType.PplAutoEmptyGoods);
            }
            UserBagItem userBagItem5 = userBagItemMap.get(eItemId.PPL_HIGH_BOX.getValue());
            if (userBagItem5 != null && userBagItem5.getNum() > 0) {
                player.getModule(CurrencyModule.class).removeCurrency(new Property(userBagItem5.getGoodsId(), BigInteger.valueOf(userBagItem5.getNum())), eLogMoneyType.Ppl, eLogMoneyType.PplAutoEmptyGoods);
            }
        }
        changePplActivityItem();*/


        // 清空剪窗花的剪刀道具（换积分）
  /*      if (JianChuangHuaMgr.isActivityInClearTime()) {
            removeActivityBagItemToScore(JianChuangHuaMgr.getActivityInfo(), JianChuangHuaMgr.scoreItemId, JianChuangHuaMgr.scissorsItemId, 0);
        }*/

        // 大富翁道具清空
      //  clearDaFuWengItem();

        // 清空苗寨道具
        //clearTrystItem();

        // 糖葫芦水果道具（换积分）
     /*   if (TangHuLuMgr.isActivityInClearTime()){
            removeActivityBagItemToScore(TangHuLuMgr.getActivityInfo(), TangHuLuMgr.SCORE_GOODS_ID, TangHuLuMgr.TANGHULU_CARAMBOLA_ID, 1 );
            removeActivityBagItemToScore(TangHuLuMgr.getActivityInfo(), TangHuLuMgr.SCORE_GOODS_ID, TangHuLuMgr.TANGHULU_HAWTHORN_ID, 1);
            removeActivityBagItemToScore(TangHuLuMgr.getActivityInfo(), TangHuLuMgr.SCORE_GOODS_ID, TangHuLuMgr.TANGHULU_KIWIFRUIT_ID, 1);
            removeActivityBagItemToScore(TangHuLuMgr.getActivityInfo(), TangHuLuMgr.SCORE_GOODS_ID, TangHuLuMgr.TANGHULU_STRAWBERRY_ID, 1);
        }
*/
        // 清空水浒卡抽卡道具
      /*  if (ActivityMgr.activityInRewardPeriod(ShuiHuMgr.getActivityInfo())) {
            removeActivityBagItemToScore(ShuiHuMgr.getActivityInfo(), eItemId.SHUIHU_SCORE_ITEM.getValue(), eItemId.SHUIHU_DRAW_ITEM.getValue(), 0);
        }*/

        // 清空弓术道具
    /*    if (BowArtMgr.isActivityInClearTime()){
            removeActivityBagItemToScore(BowArtMgr.getActivityInfo(), BowArtMgr.BOW_ART_SCORE_GOODS_ID, BowArtMgr.BOW_ART_MU_JIAN_ID, 1 );
            removeActivityBagItemToScore(BowArtMgr.getActivityInfo(), BowArtMgr.BOW_ART_SCORE_GOODS_ID, BowArtMgr.BOW_ART_TIE_JIAN_ID, 1 );
            removeActivityBagItemToScore(BowArtMgr.getActivityInfo(), BowArtMgr.BOW_ART_SCORE_GOODS_ID, BowArtMgr.BOW_ART_LIU_YE_JIAN_ID, 1 );
            removeActivityBagItemToScore(BowArtMgr.getActivityInfo(), BowArtMgr.BOW_ART_SCORE_GOODS_ID, BowArtMgr.BOW_ART_JIANG_JUN_JIAN_ID, 1 );
            removeActivityBagItemToScore(BowArtMgr.getActivityInfo(), BowArtMgr.BOW_ART_SCORE_GOODS_ID, BowArtMgr.BOW_ART_XUAN_YUAN_JIAN_ID, 1 );
        }*/

        // 清空锁妖塔抽盲盒道具
      /*  if (!ActivityMgr.activityInShowTime(SuoYaoMgr.getActivityInfo())) {
            removeActivityItemAfterEndShowTime(SuoYaoMgr.getActivityName(), eItemId.SUOYAO_DRAW_ITEM.getValue());
            removeActivityBagItemToScoreAfterEndShowTime(SuoYaoMgr.getActivityName(), eItemId.SUOYAO_SCORE_ITEM.getValue(), eItemId.SUOYAO_EXPLORE_ITEM.getValue(), 0);
        }
        if (ActivityMgr.activityInRewardPeriod(SuoYaoMgr.getActivityInfo())) {
            removeActivityBagItemToScore(SuoYaoMgr.getActivityInfo(), SuoYaoMgr.getSuoYaoConfig().getScoreItemId(), SuoYaoMgr.getSuoYaoConfig().getExploreItemId(), 0);
        }*/

        // 清空农忙季道具
       /* if (BusyFarmingMgr.isActivityInClearTime()){
            try{
                removeBusyFarmingBagItemToScore();
            }catch (Exception e){
                log.error("user {} clear busyFarming item error", getUserId(), e);
            }
        }*/

        // 大航海 清空道具
/*
        HangHaiConfig hangHaiConfig = HangHaiMgr.getHangHaiConfig();
        if (hangHaiConfig == null || ActivityMgr.activityInRewardPeriod(hangHaiConfig.getActivityInfo())) {
            int itemId = hangHaiConfig == null ? eItemId.HANGHAI_SPEED_UP_ITEM.getValue() : hangHaiConfig.getSpeedUpItemId();
            UserBagItem userBagItem1 = userBagItemMap.get(itemId);
            if (userBagItem1 != null && userBagItem1.getNum() > 0) {
                player.getModule(CurrencyModule.class).removeCurrency(new Property(userBagItem1.getGoodsId(), BigInteger.valueOf(userBagItem1.getNum())), eLogMoneyType.HangHaiActivity, eLogMoneyType.HangHaiEmptyGoods);
            }
        }
*/

        // 赛龙舟 清空道具
      /*  if (ActivityMgr.activityInShowPeriod(DragonBoatRaceMgr.activityInfo) || ActivityMgr.activityInRewardPeriod(DragonBoatRaceMgr.activityInfo)){
            for(int propId : DragonBoatRaceMgr.DRAGONBOATRACE_ENERGY_ITEM_LIST){
                removeActivityBagItemToScore(DragonBoatRaceMgr.activityInfo, DragonBoatRaceMgr.DRAGONBOATRACE_SCORE_ITEM_ID, propId, 3 );
            }
        }*/

        /*if (PawnshopMgr.isActivityInClearTime()) {
            try{
                removePawnshopItemToScore();
            }catch (Exception e){
                log.error("user {} clear busyFarming item error", getUserId(), e);
            }
        }
*/
        // 古国寻宝
 /*       if (GuGuoXunBaoMgr.isActivityInClearTime()){
            UserBagItem userBagItem = userBagItemMap.get(GuGuoXunBaoMgr.CAMEL_ID);
            if (userBagItem != null && userBagItem.getNum() > 0) {
                player.getModule(CurrencyModule.class).removeCurrency(new Property(userBagItem.getGoodsId(), BigInteger.valueOf(userBagItem.getNum())), eLogMoneyType.GuGuoXunBaoActivity, eLogMoneyType.GuGuoXunBaoActivityHuiShou);
            }
        }*/

        // 回收饮子摊道具
        /*if (ActivityMgr.activityInRewardPeriod(DrinkMgr.getActivityInfo())) {
            removeActivityBagItemToScore(DrinkMgr.getActivityInfo(), DrinkMgr.getDrinkConfig().getScoreItemId(), DrinkMgr.getDrinkConfig().getEnergyRecItemId(), 1);
        }*/
        player.getModule(DrinkModule.class).autoOrderReward();

        // 回收兰若寺道具
        /*if (ActivityMgr.activityInRewardPeriod(LanRuoSiMgr.getActivityInfo())) {
            removeActivityBagItemToScore(LanRuoSiMgr.getActivityInfo(), LanRuoSiMgr.getConfig().getScoreItemId(), LanRuoSiMgr.getConfig().getEnergyRecItemId(), 1);
        }*/

        // 清空夏越活动道具
       /* if (XiaYueMgr.inClearSearchItemPeriod()) {
            removeActivityBagItemToScoreAfterEndShowTime(XiaYueMgr.getActivityName(), eItemId.XIAYUE_SCORE_ITEM_ID.getValue(), eItemId.XIAYUE_SEARCH_ITEM_ID.getValue(), 0);
        }*/
        // 清空敦煌颜料道具
      //  clearDunHuangShiKuItem();

        // 清空周年庆典道具
       //clearCelebrationItem();

        //小当家2清除玩家活动道具
      /*  ActivityInfo cookBoy2Ac = RelationActivityMgr.getActivityInfo(eActivityType.CookingBoyActivity.getValue(),eActivityCookBoyType.Process2.getChildType());
        if(ActivityMgr.activityInRewardPeriod(cookBoy2Ac)){
            clearCookBoy2PickUpItem(cookBoy2Ac);
        }*/
        //小当家1清除恢复体力道具
       /* ActivityInfo cookBoy1Ac = RelationActivityMgr.getActivityInfo(eActivityType.CookingBoyActivity.getValue(),eActivityCookBoyType.Process1.getChildType());
        if(ActivityMgr.activityInRewardPeriod(cookBoy1Ac)){
            clearCookBoyHpItem(cookBoy1Ac);
        }
        }*/

        //遇见小面联动回收
        clearLinkageDishes(ChannelConfig.YJXM_LINKAGE_SWITCH, 50010, MailManager.YJXM_REMOVE_ITEM_TITLE, MailManager.YJXM_REMOVE_ITEM_CONTENT);
        //阳际山野联动回收
        clearLinkageDishes(ChannelConfig.YJSY_LINKAGE_SWITCH, 50011, MailManager.YJSY_REMOVE_ITEM_TITLE, MailManager.YJSY_REMOVE_ITEM_CONTENT);

        //葫芦娃道具回收
        //领奖期-七彩莲花-转积分
       /* GourdChildConfig gourdChildConfig = GourdChildMgr.getConfig();
        if(gourdChildConfig != null && ActivityMgr.activityInRewardPeriod(gourdChildConfig.getActivityInfo())){
            removeActivityBagItemToScore(gourdChildConfig.getActivityInfo(), gourdChildConfig.getScoreItemId(), gourdChildConfig.getEnergyRecItemId(), 1);
        }else if(gourdChildConfig == null || gourdChildConfig.getActivityInfo() == null || ActivityMgr.activityOverTime(gourdChildConfig.getActivityInfo())){
            //活动结束
            //清空无根水
            clearBagItem(eItemId.GOURD_CHILD_WATER_ITEM_ID.getValue(), eLogMoneyType.GourdChildGoodsRemove);
            //葫芦种子-转葫芦礼盒
            removeActivityItemAfterEndShowTime(GourdChildMgr.getActivityName(), eItemId.GOURD_CHILD_SEED_ITEM_ID.getValue());
        }

        //葫芦山道具回收
        GourdPlantConfig gourdPlantConfig = GourdPlantMgr.getConfig();
        if(gourdPlantConfig == null || ActivityMgr.activityInRewardPeriod(gourdPlantConfig.getActivityInfo())){
            removeActivityBagItemToScoreAfterEndShowTime(GourdPlantMgr.getActivityName(), eItemId.GOURD_PLANT_SCORE_ITEM.getValue(), eItemId.GOURD_PLANT_USE_ITEM.getValue(), 1);
        }*/

    /*    if(ActivityMgr.activityInRewardPeriod(HellMgr.getActivityInfo())){
            removeActivityBagItemToScoreAfterEndShowTime(HellMgr.getActivityInfo().getActivityName(), HellMgr.getHellScoreItemId(), HellMgr.getHellSearchItemId(), 0);
        }*/

        //小舞回忆碎片回收
       /* if (ActivityMgr.activityInRewardPeriod(XiaowuRecallMgr.getActivityInfo())){
            removeActivityBagItemToScore(XiaowuRecallMgr.getActivityInfo(),XiaowuRecallMgr.loadXiaowuConfigFromDb(XiaowuRecallMgr.getActivityInfo()).getSCORE_ITEM_ID(),XiaowuRecallMgr.loadXiaowuConfigFromDb(XiaowuRecallMgr.getActivityInfo()).getPIECE_ITEM_ID(),1);
        }*/
    /*    if (ActivityMgr.activityInRewardPeriod(WinterLoveMgr.getActivityInfo())){
        }
        //冬季恋歌雪铲道具回收
        if (ActivityMgr.activityInRewardPeriod(WinterLoveMgr.getActivityInfo())){
            WinterLoveConfig winterLoveConfig = WinterLoveMgr.loadWinterLoveConfigFromDb(WinterLoveMgr.getActivityInfo());
            removeActivityBagItemToScore(WinterLoveMgr.getActivityInfo(),winterLoveConfig.getScoreItemId(),winterLoveConfig.getShovelItemId(),1);
        }*/

        // 迎新夜宴道具回收
   /*     YingXinConfig yingXinConfig = YingXinMgr.getConfig();
        if (yingXinConfig == null || ActivityMgr.activityInRewardPeriod(yingXinConfig.getActivityInfo())) {
            removeActivityBagItemToScoreAfterEndShowTime(YingXinMgr.getActivityName(), eItemId.YINGXIN_SCORE_ITEM.getValue(), eItemId.YINGXIN_ENERGY_RECOVERY_ID.getValue(), 1);
        }
        if (yingXinConfig == null || !ActivityMgr.activityInShowTime(yingXinConfig.getActivityInfo())) {
            removeActivityBagItemToScoreAfterEndShowTime(YingXinMgr.getActivityName(), eItemId.YINGXIN_SCORE_ITEM.getValue(), eItemId.YINGXIN_SILVER_SPOON_ID.getValue(), 1);
            removeActivityBagItemToScoreAfterEndShowTime(YingXinMgr.getActivityName(), eItemId.YINGXIN_SCORE_ITEM.getValue(), eItemId.YINGXIN_GOLD_SPOON_ID.getValue(), 1);
        }*/
        //斗罗大陆史瑞克学院惊魂币回收
    /*    if (ActivityMgr.activityInRewardPeriod(DouLuoShrekMgr.getActivityInfo())){
            removeActivityBagItemToScoreAfterEndShowTime(DouLuoShrekMgr.getActivityInfo().getActivityName(),DouLuoShrekMgr.getCollegeScoreItemId(),DouLuoShrekMgr.getJinHunBiItemId(),0);
        }*/

        // 水浒叶子牌
        //clearShuiHuYeZiCardItem();

       /* if(ActivityMgr.activityInRewardPeriod(SpringDinnerMgr.getActivityInfo())){
            removeActivityBagItemToScoreAfterEndShowTime(SpringDinnerMgr.getActivityInfo().getActivityName(), SpringDinnerMgr.getScoreItemId(), SpringDinnerMgr.getPlayItemId(), 1);
        }*/

    }

    //     int U_ITEM_RECYCLE_SYNC = 9701; 道具回收消息同步
    private void syncRecycleItemTime(){
        BagProto.RecycleDetailSyncMsg.Builder recycleDetail = ActivityItemRecycleMgr.getRecycleDetail();
        player.sendPacket(ClientProtocol.U_ITEM_RECYCLE_SYNC,recycleDetail);
    }

    /**
     * 回收道具
     *
     * @param itemId       回收的道具id
     * @param targetItemId 转换对应的道具id
     */
    private void recycleLinkageItem(int itemId, int targetItemId) {
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(itemId);
        if (goodsInfo == null) {
            return;
        }
        UserBagItem userBagItem = userBagItemMap.get(itemId);
        if (userBagItem == null) {
            return;
        }
        long num = userBagItem.getNum();
        if (num <= 0) {
            return;
        }
        // 移除道具
        player.getModule(CurrencyModule.class).removeCurrency(new Property(userBagItem.getGoodsId(), BigInteger.valueOf(num)), eLogMoneyType.System, eLogMoneyType.LinkageRecycle);
        // 转化道具
        player.getModule(CurrencyModule.class).addCurrency(new Property(targetItemId, num), eLogMoneyType.System, eLogMoneyType.LinkageRecycle);
    }


    private void removePawnshopItemToScore() {
        ActivityInfo activityInfo = PawnshopMgr.config.getActivityInfo();
        String activityName = ActivityMgr.getActivityName(activityInfo, player.getLanguage());
        int i = 0;
        Object[] paramArr = new Object[12];
        paramArr[i++] = activityName;
        //道具id
        int goodsId = PawnshopMgr.config.staminaGoodsId;
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(goodsId);
        GoodsInfo scoreGoodsInfo = GoodsMgr.getGoodsById(PawnshopMgr.config.scoreGoodsId);

        if(goodsInfo == null || scoreGoodsInfo == null){
            log.error("remove  activity {} goods to score error,goods no found,goodsId:{}", activityInfo.getActivityId(), goodsId);
            return;
        }
        Property reward = new Property();
        Property consume = new Property();
        long num = 0;
        synchronized (userBagItemMap) {
            BigInteger addScore = goodsInfo.getParamList().get(1);
            UserBagItem bagItem = userBagItemMap.get(goodsId);
            if (bagItem == null) {
                return;
            }
            if (bagItem.getNum() <= 0) {
                return;
            }
            num = bagItem.getNum();
            consume.addProperty(goodsId,BigInteger.valueOf( bagItem.getNum()));
            addScore = addScore.multiply(BigInteger.valueOf(bagItem.getNum()));
            reward.addProperty(PawnshopMgr.config.scoreGoodsId,addScore);
            player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.YongAnDang, eLogMoneyType.YongAnDangRemoveItem);
        }

        String scoreName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), player.getLanguage());
        String scoreGoodsInfoName = ServerLanguageMgr.getContent(scoreGoodsInfo.getGoodsName(), player.getLanguage());
        paramArr[i++] = scoreName;
        paramArr[i++] = num;
        paramArr[i++] = scoreGoodsInfoName;
        String rewardStr = PropertyHelper.parsePropertyToString(reward);
        log.info("yongandang user {} rewardStr:{}", player.getUserId(), rewardStr);
        if (!reward.isNothing()) {//有将道具转换
            String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_TITLE,
                    player.getLanguage()), activityName);
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_CONTENT,
                    player.getLanguage()), paramArr);
            MailManager.sendMailOnLoad(player, eMailType.SYSTEM.getValue(), rewardStr, mailContent, mailTitle);
        }
    }

    @Override
    public boolean saveData() {
        if (null != userBagItemMap) {
            for (UserBagItem userBagItem : userBagItemMap.values()) {
                if (userBagItem.isInsertOption()) {
                    UserBagBussiness.addUserBagItem(userBagItem);
                } else if (userBagItem.isUpdateOption()) {
                    UserBagBussiness.updateUserBagItem(userBagItem);
                }
            }
        }
        return true;
    }

    @Override
    public void loginSendMsg() {
        syncRecycleItemTime();
        if (null != userBagItemMap) {
            List<UserBagItem> dataList = new ArrayList<>();
            for (UserBagItem userBagItem : userBagItemMap.values()) {
                if (userBagItem.getNum() != 0) {
                    dataList.add(userBagItem);
                }
            }
            BagProto.BagRespMsg.Builder builder = BagPb.parseBagRespMsg(dataList);
            BagPb.mergeAbleItem(builder, ActivityAbleMgr.getUserAbleItemList(player.getUserId())); // 添加凭证道具
            player.sendPacket(Protocol.U_BAG_SYNC_DATA, builder);
        }
    }

    private void onDataChanged(int goodsId) {
        synchronized (changedBagItemGoodsIdList) {
            if (!changedBagItemGoodsIdList.contains(goodsId)) {
                changedBagItemGoodsIdList.add(goodsId);
            }
        }
        if (changeCount.get() <= 0 && changedBagItemGoodsIdList.size() > 0) {
            commitDataChange();
        } else {
            if (changedBagItemGoodsIdList.size() > MAX_CHANGE_ITEM_COUNT) {
                commitDataChange();
            }
        }
    }

    private void commitDataChange() {
        changeCount.set(0);
        List<Integer> changeList;
        synchronized (changedBagItemGoodsIdList) {
            changeList = new ArrayList<>(changedBagItemGoodsIdList);
            changedBagItemGoodsIdList.clear();
        }
        if (changeList.size() > 0) {
            syncBagItem(changeList);
        }
    }

    // 同步背包信息
    private void syncBagItem(List<Integer> changeList) {
        List<UserBagItem> bagItemList = new ArrayList<>();
        for (int goodsId : changeList) {
            UserBagItem tempItem = getUserBagItem(goodsId);
            bagItemList.add(tempItem);
        }
        player.sendPacket(Protocol.U_BAG_SYNC_DATA, BagPb.parseBagRespMsg(bagItemList));
    }

    public void syncBagItem(int goodsId) {
        List<Integer> syncList = new ArrayList<>();
        syncList.add(goodsId);
        syncBagItem(syncList);
    }

    /**
     * 是否有某种物品
     */
    public boolean hasEnoughGoods(int goodsId, long count) {
        synchronized (userBagItemMap) {
            UserBagItem userBagItem = getUserBagItem(goodsId);
            BigInteger value = player.getModule(PreReduceModule.class).getPreReduceItemMap().get(goodsId);
            if (value == null) {
                value = BigInteger.ZERO;
            }
            long v = value.longValue();
            return null != userBagItem && (userBagItem.getNum() + v) >= count;
        }
    }

    public UserBagItem getUserBagItem(int goodsId) {
        return userBagItemMap.get(goodsId);
    }

    public long getUserBagItemCount(int goodsId) {
        UserBagItem userBagItem = getUserBagItem(goodsId);
        if (null != userBagItem) {
            return userBagItem.getNum();
        }
        return 0;
    }


    /**
     * 活动道具转换积分
     *
     * @param activityInfo
     * @param scoreGoodsId    积分id
     * @param removeGoodsId   移除的道具id
     * @param scoreParamIndex 道具转换积分参数下标
     */
    public void removeActivityBagItemToScore(ActivityInfo activityInfo, int scoreGoodsId, int removeGoodsId, int scoreParamIndex) {
        Property reward;
        long count;
        GoodsInfo goodsInfo;
        GoodsInfo scoreGoodsInfo;
        synchronized (userBagItemMap) {
            UserBagItem userBagItem = getUserBagItem(removeGoodsId);
            if (null == userBagItem || userBagItem.getNum() <= 0) {
                return;
            }
            count = userBagItem.getNum();

            if (ActivityMgr.activityInTime(activityInfo)) {//在活动进行期间不自动消耗
                return;
            }
            if (null == activityInfo) {
                return;
            }
            long now = System.currentTimeMillis() / 1000;
            if (activityInfo.getBeginShowTime() <= now && activityInfo.getEndTime() > now) {
                return;
            }
            goodsInfo = GoodsMgr.getGoodsById(removeGoodsId);
            if (null == goodsInfo) {
                log.error("remove  activity goods to score error,goods no found,goodsId:{},remove count:{}", removeGoodsId, count);
                return;
            }
            scoreGoodsInfo = GoodsMgr.getGoodsById(scoreGoodsId);
            if (null == scoreGoodsInfo) {
                log.error("remove  activity goods to score error, score goods no found,goodsId:{},remove count:{}", removeGoodsId, count);
                return;
            }
            BigInteger score = goodsInfo.getParamList().get(scoreParamIndex);       //参数x为转换积分
            if (score.compareTo(BigInteger.ZERO) <= 0) {
                log.error("remove  activity goods to score error,goodsId:{},remove count:{}", removeGoodsId, count);
                return;
            }
            score = score.multiply(BigInteger.valueOf(count));
            reward = new Property(scoreGoodsId, score);
            userBagItem.setNum(0);
            onDataChanged(removeGoodsId);
        }
        String rewardStr = PropertyHelper.parsePropertyToString(reward);
        if (!StringUtils.isNullOrEmpty(rewardStr)) {//有将道具转换
            String goodsName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), player.getLanguage());
            String activityName = ActivityMgr.getActivityName(activityInfo, player.getLanguage());
            String scoreName = ServerLanguageMgr.getContent(scoreGoodsInfo.getGoodsName(), player.getLanguage());
            String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_TITLE,
                    player.getLanguage()), activityName);
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_CONTENT,
                    player.getLanguage()), activityName, goodsName, count, scoreName);
            MailManager.sendMailOnLoad(player, eMailType.SYSTEM.getValue(), rewardStr, mailContent, mailTitle);
        } else {
            if (count > 0) {
                log.error("remove  activity goods to score reward is empty,consume goodsId:{},count:{}", removeGoodsId, count);
            }
        }
    }

    /**
     * 活动结束后清空道具换积分
     *
     * @param tmpActivityName 活动名称（未多语言转换）
     * @param scoreGoodsId    积分id
     * @param removeGoodsId   移除的道具id
     * @param scoreParamIndex 道具转换积分参数下标
     */
    public void removeActivityBagItemToScoreAfterEndShowTime(String tmpActivityName, int scoreGoodsId, int removeGoodsId, int scoreParamIndex) {
        Property reward;
        long count;
        GoodsInfo goodsInfo;
        GoodsInfo scoreGoodsInfo;
        synchronized (userBagItemMap) {
            UserBagItem userBagItem = getUserBagItem(removeGoodsId);
            if (null == userBagItem || userBagItem.getNum() <= 0) {
                return;
            }
            count = userBagItem.getNum();

            goodsInfo = GoodsMgr.getGoodsById(removeGoodsId);
            if (null == goodsInfo) {
                log.error("remove  activity goods to score error,goods no found,goodsId:{},remove count:{}", removeGoodsId, count);
                return;
            }
            scoreGoodsInfo = GoodsMgr.getGoodsById(scoreGoodsId);
            if (null == scoreGoodsInfo) {
                log.error("remove  activity goods to score error, score goods no found,goodsId:{},remove count:{}", removeGoodsId, count);
                return;
            }
            BigInteger score = goodsInfo.getParamList().get(scoreParamIndex);       //参数x为转换积分
            if (score.compareTo(BigInteger.ZERO) <= 0) {
                log.error("remove  activity goods to score error,goodsId:{},remove count:{}", removeGoodsId, count);
                return;
            }
            score = score.multiply(BigInteger.valueOf(count));
            reward = new Property(scoreGoodsId, score);
            userBagItem.setNum(0);
            onDataChanged(removeGoodsId);
        }
        String rewardStr = PropertyHelper.parsePropertyToString(reward);
        if (!StringUtils.isNullOrEmpty(rewardStr)) {//有将道具转换
            String goodsName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), player.getLanguage());
            String activityName = ServerLanguageMgr.getContent(tmpActivityName, player.getLanguage());
            String scoreName = ServerLanguageMgr.getContent(scoreGoodsInfo.getGoodsName(), player.getLanguage());
            String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_TITLE,
                    player.getLanguage()), activityName);
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_CONTENT,
                    player.getLanguage()), activityName, goodsName, count, scoreName);
            MailManager.sendMailOnLoad(player, eMailType.SYSTEM.getValue(), rewardStr, mailContent, mailTitle);
        } else {
            if (count > 0) {
                log.error("remove  activity goods to score reward is empty,consume goodsId:{},count:{}", removeGoodsId, count);
            }
        }
    }


    /**
     * 活动结束后清空道具换道具
     */
    public void removeActivityItemAfterEndShowTime(String tmpActivityName, int removeItemId) {
        Property reward;
        long removeCount;
        GoodsInfo removeGoodsInfo;
        GoodsInfo toGoodsInfo;
        synchronized (userBagItemMap) {
            UserBagItem removeItem = getUserBagItem(removeItemId);
            if (null == removeItem || removeItem.getNum() <= 0) {
                return;
            }
            removeCount = removeItem.getNum();
            removeGoodsInfo = GoodsMgr.getGoodsById(removeItemId);
            if (null == removeGoodsInfo) {
                log.error("remove Activity Item After End Show Time error, removeItem no found, removeGoodsId:{}, remove count:{}", removeItemId, removeCount);
                return;
            }
            toGoodsInfo = GoodsMgr.getGoodsById(Integer.valueOf(removeGoodsInfo.getExtendParam()));
            if (null == toGoodsInfo) {
                log.error("remove Activity Item After End Show Time error, toItem goods no found, removeGoodsId:{}, remove count:{}", removeItemId, removeCount);
                return;
            }
            reward = new Property(toGoodsInfo.getGoodsId(), BigInteger.valueOf(removeCount));
            removeItem.setNum(0);
            onDataChanged(removeItemId);
        }
        String rewardStr = PropertyHelper.parsePropertyToString(reward);
        if (!StringUtils.isNullOrEmpty(rewardStr)) {//有将道具转换
            String removeName = ServerLanguageMgr.getContent(removeGoodsInfo.getGoodsName(), player.getLanguage());
            String activityName = ServerLanguageMgr.getContent(tmpActivityName, player.getLanguage());
            String toName = ServerLanguageMgr.getContent(toGoodsInfo.getGoodsName(), player.getLanguage());
            String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_TITLE,
                    player.getLanguage()), activityName);
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_CONTENT,
                    player.getLanguage()), activityName, removeName, removeCount, toName);
            MailManager.sendMailOnLoad(player, eMailType.SYSTEM.getValue(), rewardStr, mailContent, mailTitle);
        } else {
            if (removeCount > 0) {
                log.error("remove Activity Item After End Show Time error, removeItemId={}, removeCount={}, but no reward!", removeItemId, removeCount);
            }
        }
    }

    /**
     * 活动道具转换积分
     *
     * @param activityInfo
     * @param scoreGoodsId    积分id
     * @param removeGoodsId   移除的道具id
     * @param scoreParamIndex 道具转换积分参数下标
     * @param emailTitle      邮件标题
     * @param emailContent    邮件内容
     */
    public void removeActivityBagItemToScore(ActivityInfo activityInfo, int scoreGoodsId, int removeGoodsId, int scoreParamIndex, String emailTitle, String emailContent) {
        Property reward;
        long count;
        GoodsInfo goodsInfo;
        synchronized (userBagItemMap) {
            UserBagItem userBagItem = getUserBagItem(removeGoodsId);
            if (null == userBagItem || userBagItem.getNum() <= 0) {
                return;
            }
            count = userBagItem.getNum();

            if (ActivityMgr.activityInTime(activityInfo)) {//在活动进行期间不自动消耗
                return;
            }
            if (null == activityInfo) {
                return;
            }
            long now = System.currentTimeMillis() / 1000;
            if (activityInfo.getBeginShowTime() <= now && activityInfo.getEndTime() > now) {
                return;
            }
            goodsInfo = GoodsMgr.getGoodsById(removeGoodsId);
            if (null == goodsInfo) {
                log.error("remove  activity goods to score error,goods no found,goodsId:{},remove count:{}", removeGoodsId, count);
                return;
            }
            BigInteger score = goodsInfo.getParamList().get(scoreParamIndex);       //参数x为转换积分
            if (score.compareTo(BigInteger.ZERO) <= 0) {
                log.error("remove  activity goods to score error,goodsId:{},remove count:{}", removeGoodsId, count);
                return;
            }
            score = score.multiply(BigInteger.valueOf(count));
            reward = new Property(scoreGoodsId, score);
            userBagItem.setNum(0);
            onDataChanged(removeGoodsId);
        }
        String rewardStr = PropertyHelper.parsePropertyToString(reward);
        if (!StringUtils.isNullOrEmpty(rewardStr)) {//有将道具转换

            String mailTitle = emailTitle;
            String mailContent = emailContent;
            if (emailTitle.equals(MailManager.TREASURE_ACTIVITY_GOODS_AUTO_CONSUME_TITLE)
                    || emailTitle.equals(MailManager.YEAR_MASTER_ACTIVITY_GOODS_AUTO_CONSUME_TITLE)
                    || emailTitle.equals(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_TITLE)) {
                //"{0}活动";
                //"亲爱的掌柜，由于您在上次{0}活动中，{1}×{2} 未及时使用，现已为您自动转换活动积分，敬请查收！";
                String goodsName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), player.getLanguage());
                String activityName = ActivityMgr.getActivityName(activityInfo, player.getLanguage());
                mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(emailTitle,
                        player.getLanguage()), activityName);
                mailContent = StringUtils.format(MultipleLanguageMgr.getContent(emailContent,
                        player.getLanguage()), activityName, goodsName, count);

                if (emailTitle.equals(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_TITLE)) {
                    GoodsInfo scoreGoodInfo = GoodsMgr.getGoodsById(scoreGoodsId);
                    mailContent = StringUtils.format(MultipleLanguageMgr.getContent(emailContent,
                            player.getLanguage()), activityName, goodsName, count, ServerLanguageMgr.getContent(scoreGoodInfo.getGoodsName(), player.getLanguage()));
                }
            } else {
                //其他格式
            }
            MailManager.sendMailOnLoad(player, eMailType.SYSTEM.getValue(), rewardStr, mailContent, mailTitle);
        } else {
            if (count > 0) {
                log.error("remove  activity goods to score reward is empty,consume goodsId:{},count:{}", removeGoodsId, count);
            }
        }
    }

    /**
     * 消耗选美大会活动道具
     */
    public void removePageantActivityBagItem(ActivityInfo activityInfo, int scoreGoodsId, int removeGoodsId) {
        Property reward;
        long count;
        long modifyTime;
        GoodsInfo goodsInfo;
        synchronized (userBagItemMap) {
            UserBagItem userBagItem = getUserBagItem(removeGoodsId);
            if (null == userBagItem || userBagItem.getNum() <= 0) {
                return;
            }
            count = userBagItem.getNum();
            modifyTime = userBagItem.getModifyTime();
            if (ActivityMgr.activityInTime(activityInfo)) {//在活动进行期间不自动消耗
                return;
            }
            if (null == activityInfo) {
                return;
            }
            long now = System.currentTimeMillis() / 1000;
            if (activityInfo.getBeginShowTime() <= now && activityInfo.getEndTime() > now) {
                return;
            }
            goodsInfo = GoodsMgr.getGoodsById(removeGoodsId);
            if (null == goodsInfo) {
                log.error("remove pageant activity goods to score error,goods no found,goodsId:{},remove count:{}", removeGoodsId, count);
                return;
            }
            BigInteger score = goodsInfo.getParamList().get(2);       //参数3为转换积分
            if (score.compareTo(BigInteger.ZERO) <= 0) {
                log.error("remove pageant activity goods to score error,goodsId:{},remove count:{}", removeGoodsId, count);
                return;
            }
            score = score.multiply(BigInteger.valueOf(count));
            reward = new Property(scoreGoodsId, score);
            userBagItem.setNum(0);
            onDataChanged(removeGoodsId);
        }
        String rewardStr = PropertyHelper.parsePropertyToString(reward);
        if (!StringUtils.isNullOrEmpty(rewardStr)) {//有将道具转换
            String goodsName = MultipleLanguageMgr.getContent(goodsInfo.getGoodsName(), player.getLanguage());
            String activityName = ActivityMgr.getActivityName(activityInfo, player.getLanguage());
            String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.PAGEANT_ACTIVITY_GOODS_AUTO_CONSUME_TITLE,
                    player.getLanguage()), activityName);
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.PAGEANT_ACTIVITY_GOODS_AUTO_CONSUME_CONTENT,
                    player.getLanguage()), activityName, goodsName, count);
            MailManager.sendMailOnLoad(player, eMailType.SYSTEM.getValue(), rewardStr, mailContent, mailTitle);
        } else {
            if (count > 0) {
                log.error("remove pageant activity goods to score reward is empty,consume goodsId:{},count:{}", removeGoodsId, count);
            }
        }
    }

    public void removeHolidayActivityBagItem(ActivityInfo activityInfo, int goodsId) {
        Property reward = new Property();
        long count;
        BigInteger turnScore;
        synchronized (userBagItemMap) {
            UserBagItem userBagItem = getUserBagItem(goodsId);
            if (null == userBagItem || userBagItem.getNum() <= 0) {
                return;
            }
            if (ActivityMgr.activityInTime(activityInfo)) {//在活动进行期间不自动消耗
                return;
            }
            count = userBagItem.getNum();
            BigInteger petScore = GoodsMgr.getGoodsById(goodsId).getParamList().get(0);
            if (petScore.compareTo(BigInteger.ZERO) <= 0) {
                log.error("remove holiday activity goods to score,petScore config error,goodsId:{},remove count:{}", goodsId, count);
                return;
            }
            List<Integer> param4List = HolidayActivityMgr.getActivityParam4(activityInfo);
            turnScore = petScore.multiply(BigInteger.valueOf(count));
            reward.setGood(param4List.get(1), turnScore);
            removeBagItem(goodsId, count, eLogMoneyType.HolidayActivityAutoUseGoods);
        }
        if (count <= 0) {
            return;
        }
        String rewardStr = PropertyHelper.parsePropertyToString(reward);
        String goodsName = "";
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(goodsId);
        if (null != goodsInfo) {
            goodsName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), player.getLanguage());
        } else {
            log.error("remove holiday activity goods to score,goods no find,goodsId:{},remove count:{}", goodsId, count);
        }
        String activityName = ActivityMgr.getActivityName(activityInfo, player.getLanguage());
        String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.HOLIDAY_ACTIVITY_GOODS_AUTO_CONSUME_TITLE, player.getLanguage()),
                activityName);
        String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.HOLIDAY_ACTIVITY_GOODS_AUTO_CONSUME_CONTENT, player.getLanguage()),
                activityName, goodsName, count, turnScore);
        MailManager.sendMailOnLoad(player, eMailType.SYSTEM.getValue(), rewardStr, mailContent, mailTitle);
    }

    public void removeCommonActivityBagItem(ActivityInfo activityInfo, int goodsId) {
        Property reward = new Property();
        long count;
        long modifyTime;
        synchronized (userBagItemMap) {
            UserBagItem userBagItem = getUserBagItem(goodsId);
            if (null == userBagItem || userBagItem.getNum() <= 0) {
                return;
            }
            count = userBagItem.getNum();
            modifyTime = userBagItem.getModifyTime();
            if (ActivityMgr.activityInTime(activityInfo)) {//在活动进行期间不自动消耗
                return;
            }
            for (int i = 0; i < count; i++) {
                OpenGoodsBagResult result = GoodsBagMgr.getRandomGoods(goodsId);
                reward.addProperty(result.getReward());
            }
            userBagItem.setNum(0);
            onDataChanged(goodsId);
        }
        String rewardStr = PropertyHelper.parsePropertyToString(reward);
        if (!StringUtils.isNullOrEmpty(rewardStr)) {//有将道具转换
            String goodsName = "";
            GoodsInfo goodsInfo = GoodsMgr.getGoodsById(goodsId);
            if (null != goodsInfo) {
                goodsName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), player.getLanguage());
            } else {
                log.error("remove common activity goods to score,goods no find,goodsId:{},remove count:{}", goodsId, count);
            }
            String activityName = ActivityMgr.getActivityName(activityInfo, player.getLanguage());
            String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_ACTIVITY_GOODS_AUTO_CONSUME_TITLE, player.getLanguage()),
                    activityName);
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_ACTIVITY_GOODS_AUTO_CONSUME_CONTENT, player.getLanguage()),
                    activityName, goodsName, count);
            MailManager.sendMailOnLoad(player, eMailType.SYSTEM.getValue(), rewardStr, mailContent, mailTitle);
        } else {
            if (count > 0) {
                log.error("remove common activity goods to score reward is empty,consume goodsId:{},count:{}", goodsId, count);
            }
        }
        LogMgr.addLogCommonActivityGoodsAutoConsume(player.getUserId(), goodsId, count, modifyTime, rewardStr);
    }

    /**
     * 清空农忙季道具
     */
    public void removeBusyFarmingBagItemToScore() {
        ActivityInfo activityInfo = BusyFarmingMgr.getActivityInfo();
        String activityName = ActivityMgr.getActivityName(activityInfo, player.getLanguage());
        int i = 0;
        Object[] paramArr = new Object[12];
        paramArr[i++] = activityName;
        //水果币
        int farmingFruitScoreId = BusyFarmingMgr.BUSY_FARMING_FRUIT_SCORE_ID;
        GoodsInfo scoreGoodsInfo = GoodsMgr.getGoodsById(farmingFruitScoreId);
        if(scoreGoodsInfo == null){
            log.error("remove  activity {} goods to score error,goods no found,goodsId:{}", activityInfo.getActivityId(), farmingFruitScoreId);
            return;
        }
        Property reward = new Property();
        Property consume = new Property();
        boolean removed = false;
        synchronized (userBagItemMap) {
            for (int removeGoodsId : BusyFarmingMgr.getBusyFarmingSeedGoods()) {
                UserBagItem userBagItem = getUserBagItem(removeGoodsId);
                long count = 0;
                if (null != userBagItem && userBagItem.getNum() > 0) {
                    count = userBagItem.getNum();
                }
                if(count > 0) {
                    consume.addProperty(removeGoodsId, BigInteger.valueOf(count));
                }
                GoodsInfo goodsInfo = GoodsMgr.getGoodsById(removeGoodsId);
                if (goodsInfo != null) {
                    long score = BusyFarmingMgr.getSeedMaturityFruitScore(removeGoodsId, BusyFarmingMgr.BUSY_FARMING_RECOVERY_DISCOUNT, (int)count);
                    reward.addProperty(farmingFruitScoreId, BigInteger.valueOf(score));
                    String goodsName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), player.getLanguage());
                    paramArr[i++] = goodsName;
                    paramArr[i++] = count;
                }
            }
            int farmingManureId = BusyFarmingMgr.BUSY_FARMING_MANURE_ID;
            UserBagItem userBagItem = getUserBagItem(farmingManureId);
            long count = 0;
            if (null != userBagItem && userBagItem.getNum() > 0) {
                count = userBagItem.getNum();
            }
            if(count > 0) {
                consume.addProperty(farmingManureId, BigInteger.valueOf(count));
            }
            GoodsInfo goodsInfo = GoodsMgr.getGoodsById(farmingManureId);
            if (goodsInfo != null) {
                long score = BigDecimal.valueOf(goodsInfo.getParamList().get(1).longValue()).multiply(BigDecimal.valueOf(count))
                        .multiply(BigDecimal.valueOf((long)BusyFarmingMgr.BUSY_FARMING_RECOVERY_DISCOUNT)).divide(BigDecimal.valueOf(1000), 0, BigDecimal.ROUND_UP).longValue();
                reward.addProperty(farmingFruitScoreId, BigInteger.valueOf(score));
                String goodsName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), player.getLanguage());
                paramArr[i++] = goodsName;
                paramArr[i++] = count;
            }
            if(!consume.isNothing()) {
                removed = player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.BusyFarmingActivity, eLogMoneyType.BusyFarmingActivityHuiShou);
                log.info("user {} huishou goods : {}", player.getUserId(), PropertyHelper.parsePropertyToString(consume));
            }
        }

        String rewardStr = PropertyHelper.parsePropertyToString(reward);
        log.info("user {} rewardStr:{}", player.getUserId(), rewardStr);
        if (!reward.isNothing() && removed) {//有将道具转换
            String scoreName = ServerLanguageMgr.getContent(scoreGoodsInfo.getGoodsName(), player.getLanguage());
            paramArr[i++] = scoreName;
            String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.BUSY_FARMING_ACTIVITY_GOODS_HUISHOU_TITLE,
                    player.getLanguage()), activityName);
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.BUSY_FARMING_ACTIVITY_GOODS_HUISHOU_CONTENT,
                    player.getLanguage()), paramArr);
            MailManager.sendMailOnLoad(player, eMailType.SYSTEM.getValue(), rewardStr, mailContent, mailTitle);
        }
    }

    /**
     * @param goodsId
     * @param count
     * @param removeType 移除记录类型
     *                   背包类型
     * @return 0 输入的值为0，跟扣除的数量相同则代表扣除成功，负数代表扣除失败
     */
    public long removeBagItem(int goodsId, long count, eLogMoneyType removeType) {
        if (count < 0) {
            return 0;
        }
        synchronized (userBagItemMap) {
            UserBagItem userBagItem = getUserBagItem(goodsId);
            if (null == userBagItem || userBagItem.getNum() <= 0) {
                return -1;
            }
            if (userBagItem.getNum() >= count) {
                userBagItem.setNum(userBagItem.getNum() - count);
                userBagItem.setModifyTime(System.currentTimeMillis() / 1000);
                onDataChanged(goodsId);
                LogMgr.addLogGoods(goodsId, player.getUserId(), false, count, userBagItem.getNum(), removeType, player.getUserInfo().getVipExp());
                return count;
            }
        }
        return -1;
    }

    /**
     * @param goodsId
     * @param removeType 移除记录类型
     *                   背包类型
     * @return 0 输入的值为0，跟扣除的数量相同则代表扣除成功，负数代表扣除失败
     */
    public long removeBagItemCanNegative(int goodsId, long count, eLogMoneyType removeType) {
        if (count < 0) {
            return 0;
        }
        synchronized (userBagItemMap) {
            UserBagItem userBagItem = getUserBagItem(goodsId);
            if (null == userBagItem) {
                return -1;
            }
            userBagItem.setNum(userBagItem.getNum() - count);
            userBagItem.setModifyTime(System.currentTimeMillis() / 1000);
            onDataChanged(goodsId);
            LogMgr.addLogGoods(goodsId, player.getUserId(), false, count, userBagItem.getNum(), removeType, player.getUserInfo().getVipExp());
            return count;
        }
    }



    public void clearBagItem(int goodsId, eLogMoneyType removeType) {
        synchronized (userBagItemMap) {
            UserBagItem userBagItem = getUserBagItem(goodsId);
            if (null == userBagItem || userBagItem.getNum() <= 0) {
                return;
            }
            long count = userBagItem.getNum();
            userBagItem.setNum(0);
            userBagItem.setModifyTime(System.currentTimeMillis() / 1000);
            onDataChanged(goodsId);
            LogMgr.addLogGoods(goodsId, player.getUserId(), false, count, userBagItem.getNum(), removeType, player.getUserInfo().getVipExp());
        }
    }

    /**
     * @param goodsId
     * @param num     需要添加的数目
     * @return 返回值为剩下未添加的物品数量
     */
    public long addBagItem(int goodsId, long num, eLogMoneyType getType) {
        if (null == userBagItemMap) {
            return num;
        }
        if (num <= 0) {
            log.warn("addBagItem count:{}", num);
            return num;
        }
        synchronized (userBagItemMap) {
            UserBagItem userBagItem = getUserBagItem(goodsId);
            if (null == userBagItem) {
                userBagItem = new UserBagItem();
                userBagItem.setUserId(player.getUserId());
                userBagItem.setGoodsId(goodsId);
                userBagItem.setInsertOption();
                userBagItemMap.put(userBagItem.getGoodsId(), userBagItem);
            }
            long oldNum = userBagItem.getNum();
            userBagItem.setNum(userBagItem.getNum() + num);
            userBagItem.setModifyTime(System.currentTimeMillis() / 1000);
            onDataChanged(goodsId);
            LogMgr.addLogGoods(goodsId, player.getUserId(), true, num, userBagItem.getNum(), getType, player.getUserInfo().getVipExp());
            player.getModule(GoodsResourceModule.class).addGoodsFirstGetData(goodsId,System.currentTimeMillis());
            if (ActivityMgr.activityInTime(DunHuangShiKuMgr.activityInfo)) {
                // 在活动时间内才需要
                player.getModule(DunHuangShiKuModule.class).changeItemNum(DunHuangShiKuMgr.activityInfo.getActivityId(),goodsId, num);
            }

            if (goodsId == eItemId.DOULUO_TRIED_STONE.getValue()) {
                if (ActivityMgr.activityInShowTime(DouLuoDaLuMgr.getActivityInfo())) {
                    player.getModule(DouLuoDaLuModule.class).addTriedStoneNum(num);
                }
            }

            player.getModule(SuiTangModule.class).syncRemainMuBingNum(goodsId);
            return 0;
        }
    }

    public void changeBagItem(int goodsId, long num, eLogMoneyType getType) {
        if (null == userBagItemMap) {
            return;
        }
        if (num < 0) {
            log.warn("changeBagItem count:{}", num);
            return;
        }
        synchronized (userBagItemMap) {
            UserBagItem userBagItem = getUserBagItem(goodsId);
            if (null == userBagItem) {
                userBagItem = new UserBagItem();
                userBagItem.setUserId(player.getUserId());
                userBagItem.setGoodsId(goodsId);
                userBagItem.setInsertOption();
                userBagItemMap.put(userBagItem.getGoodsId(), userBagItem);
            }
            long oldNum = userBagItem.getNum();
            if (oldNum == num) return;
            userBagItem.setNum(num);
            userBagItem.setModifyTime(System.currentTimeMillis() / 1000);
            onDataChanged(goodsId);
            LogMgr.addLogGoods(goodsId, player.getUserId(), true, num - oldNum, userBagItem.getNum(), getType, player.getUserInfo().getVipExp());
        }
    }

    public long addBagValueItem(int goodsId, long num, int $type, eLogMoneyType getType) {
        if (null == userBagItemMap) {
            return num;
        }
        synchronized (userBagItemMap) {
            UserBagItem userBagItem = getUserBagItem(goodsId);
            if (null == userBagItem) {
                userBagItem = new UserBagItem();
                userBagItem.setUserId(player.getUserId());
                userBagItem.setGoodsId(goodsId);
                userBagItem.setInsertOption();
                userBagItemMap.put(userBagItem.getGoodsId(), userBagItem);
            }
            long setValue = 0;
            long oldNum = userBagItem.getNum();
            if ($type == 1) {//只能扣除到0
                setValue = oldNum - num < 0 ? 0 : oldNum - num;
            } else if ($type == 2) {//可以扣除到负数
                setValue = oldNum - num;
            } else {
                setValue = oldNum - num;
            }
            userBagItem.setNum(setValue);
            userBagItem.setModifyTime(System.currentTimeMillis() / 1000);
            onDataChanged(goodsId);
            LogMgr.addLogGoods(goodsId, player.getUserId(), true, num, userBagItem.getNum(), getType, player.getUserInfo().getVipExp());
            return 0;
        }
    }

    private void changePplActivityItem() {
        if (PplMgr.isActivityInRewardTime()) {
            ActivityInfo activityInfo = PplMgr.getActivityInfo();
            UserBagItem userBagItem = userBagItemMap.get(PplMgr.PPL_ENERGY_RECOVERY_ID);
            GoodsInfo goodsInfo = CommonMgr.getGoodsInfo(PplMgr.PPL_ENERGY_RECOVERY_ID);
            GoodsInfo goodsInfo2 = CommonMgr.getGoodsInfo(PplMgr.PPL_SCORE_GOODS_ID);
            if (userBagItem == null || goodsInfo == null || goodsInfo2 == null) {
                log.error("changePplActivityItem error! UserBagItem or GoodsInfo is null!");
                return;
            }
            String activityName = ActivityMgr.getActivityName(activityInfo, player.getLanguage());
            String goodsName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), player.getLanguage());
            String goodsName2 = ServerLanguageMgr.getContent(goodsInfo2.getGoodsName(), player.getLanguage());
            if (userBagItem.getNum() > 0) {
                long num = userBagItem.getNum();
                player.getModule(CurrencyModule.class).removeCurrency(new Property(userBagItem.getGoodsId(), BigInteger.valueOf(num)), eLogMoneyType.Ppl, eLogMoneyType.PplAutoUseGoods);
                Property property = new Property(PplMgr.PPL_SCORE_GOODS_ID, BigInteger.valueOf(num * goodsInfo.getParamList().get(1).intValue()));
                String title = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_TITLE, player.getLanguage()), activityName);
                String content = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_CONTENT, player.getLanguage()),
                        activityName, goodsName, num, goodsName2);
                MailManager.sendMail(player.getUserId(), eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(property), content, title);
            }
        }
    }

    /**
     * 清空苗寨道具
     */
    public void clearTrystItem() {
        try {
            Set<Integer> needRemoveIdList = new HashSet<>();
            needRemoveIdList.add(eItemId.FireCracker.getValue());
            needRemoveIdList.add(eItemId.HUO_BA.getValue());
            needRemoveIdList.add(eItemId.CHA_BEI.getValue());
            needRemoveIdList.add(eItemId.SHENGDAN_LIWU.getValue());
            needRemoveIdList.add(eItemId.HONG_DOU.getValue());
            needRemoveIdList.add(eItemId.HUO_CUN.getValue());
            needRemoveIdList.add(eItemId.YING_HUA.getValue());
            needRemoveIdList.add(eItemId.HUO_ZHU.getValue());
            needRemoveIdList.add(eItemId.MO_DNG.getValue());
            needRemoveIdList.add(eItemId.BO_LANG_GU.getValue());
            needRemoveIdList.add(eItemId.YIN_SHI.getValue());
            needRemoveIdList.add(eItemId.ZHONG_WU_ZHI_HUN.getValue());
            needRemoveIdList.add(eItemId.JIN_LI_QI.getValue());
            needRemoveIdList.add(eItemId.YIN_ZAN.getValue());
            needRemoveIdList.add(eItemId.LIE_JIU.getValue());
            needRemoveIdList.add(eItemId.XIAN_TUAN.getValue());
            needRemoveIdList.add(eItemId.SHENG_SUO.getValue());
            needRemoveIdList.add(eItemId.GE_MI_DAO.getValue());
            needRemoveIdList.add(eItemId.CHOU_MA.getValue());
            needRemoveIdList.add(eItemId.KE_DAO.getValue());
            needRemoveIdList.add(eItemId.AI_XIN.getValue());
            needRemoveIdList.add(eItemId.BIE_JIA_ZHEN.getValue());
            needRemoveIdList.add(eItemId.CHUAN_JIAN.getValue());
            needRemoveIdList.add(eItemId.HUO_GUAN.getValue());
            needRemoveIdList.add(eItemId.MIAN_JU.getValue());
            needRemoveIdList.add(eItemId.XI_QUE.getValue());
            needRemoveIdList.add(eItemId.CAO_MAO.getValue());
            needRemoveIdList.add(eItemId.GAI_ZHUI.getValue());
            needRemoveIdList.add(eItemId.DIAO_HUA_DAO.getValue());
            needRemoveIdList.add(eItemId.QING_HUA.getValue());
            needRemoveIdList.add(eItemId.SHUI_NANG.getValue());
            needRemoveIdList.add(eItemId.QIU_BANG.getValue());
            needRemoveIdList.add(eItemId.YAN_HUA.getValue());
            needRemoveIdList.add(eItemId.YUE_GUI_YE.getValue());
            needRemoveIdList.add(eItemId.SUAN_HE_AI_HAO.getValue());
            needRemoveIdList.add(eItemId.JIAN_SHI.getValue());
            needRemoveIdList.add(eItemId.SHUI_DENG.getValue());
            needRemoveIdList.add(eItemId.HUAN_FENG_DI.getValue());
            needRemoveIdList.add(eItemId.MEI_GUI_HUA.getValue());
            Set<Integer> newFilterSet = GoodsMgr.getGoodsMap().values().stream()
                    .filter(goodsInfo -> goodsInfo.getType() == eGoodsType.SkinActivityItem.getValue())
                    .map(GoodsInfo::getGoodsId)
                    .collect(Collectors.toSet());
            needRemoveIdList.addAll(newFilterSet);
            for (ActivityInfo activityInfo : NormalActivityMgr.getTrystActivityInfoList()) {
                int itemId = Integer.parseInt(activityInfo.getParam4().split(";")[0]);
                needRemoveIdList.remove(itemId);
                if (activityInfo.getChildType() == 6) {
                    needRemoveIdList.remove(eItemId.FireCracker.getValue());
                }
                if (activityInfo.getParam4().split(";").length >= 3) {
                    int disperseGoldItem = Integer.parseInt(activityInfo.getParam4().split(";")[2]);
                    if (disperseGoldItem != 0) {
                        needRemoveIdList.remove(disperseGoldItem);
                    }
                }
            }
            for (Integer itemId : needRemoveIdList) {
                UserBagItem userBagItem = userBagItemMap.get(itemId);
                if (userBagItem != null && userBagItem.getNum() > 0) {
                    player.getModule(CurrencyModule.class).removeCurrency(new Property(userBagItem.getGoodsId(), BigInteger.valueOf(userBagItem.getNum())), eLogMoneyType.Activity, eLogMoneyType.TrystHuiShou);
                }
            }
        } catch (Exception e) {
            log.error("user {} clear tryst item error", getUserId(), e);
        }

    }

    /**
     * 清空大富翁道具
     */
    public void clearDaFuWengItem() {
        DFWConfig dfwConfig = DaFuWengMgr.getDfwConfig();
        if (dfwConfig != null && (ActivityMgr.activityInShowPeriod(dfwConfig.getActivityInfo()) || ActivityMgr.activityInRewardPeriod(dfwConfig.getActivityInfo()))) {
            synchronized (userBagItemMap) {
                String activityName = ActivityMgr.getActivityName(dfwConfig.getActivityInfo(), player.getLanguage());
                String energyItemName = "";
                int energyItemCount = 0;
                int cardCount = 0;
                String scoreItemName = "";

                Property getProp = new Property();
                Property removeProp = new Property();

                // 体力道具换积分
                GoodsInfo energyGoodsInfo = GoodsMgr.getGoodsById(eItemId.DFW_ENERGY_ITEM.getValue());
                UserBagItem energyUserBagItem = userBagItemMap.get(eItemId.DFW_ENERGY_ITEM.getValue());
                if (energyGoodsInfo != null) {
                    energyItemName = ServerLanguageMgr.getContent(energyGoodsInfo.getGoodsName(), player.getLanguage());
                }
                if (energyGoodsInfo != null && energyUserBagItem != null && energyUserBagItem.getNum() > 0) {
                    energyItemCount = (int) energyUserBagItem.getNum();
                    removeProp.addProperty(eItemId.DFW_ENERGY_ITEM.getValue(), BigInteger.valueOf(energyItemCount));
                    getProp.addProperty(eItemId.DFW_GOLDEN.getValue(), energyGoodsInfo.getParamList().get(1).multiply(BigInteger.valueOf(energyItemCount)));
                }

                // 卡片也换积分
                List<Integer> cardIdList = new ArrayList<>();
                cardIdList.add(eItemId.DFW_GOLD_CARD.getValue());
                cardIdList.add(eItemId.DFW_YAO_YAN_CARD.getValue());
                cardIdList.add(eItemId.DFW_ZHU_FU_CARD.getValue());
                cardIdList.add(eItemId.DFW_ENERGY_CARD.getValue());
                cardIdList.add(eItemId.DFW_LU_ZHANG_CARD.getValue());
                cardIdList.add(eItemId.DFW_YONG_BING_CARD.getValue());
                cardIdList.add(eItemId.DFW_GOD_CARD.getValue());
                cardIdList.add(eItemId.DFW_HUO_WU_CARD.getValue());
                cardIdList.add(eItemId.DFW_ZHUI_BU_CARD.getValue());
                cardIdList.add(eItemId.DFW_RANDOM_CARD.getValue());
                for (Integer cardId : cardIdList) {
                    UserBagItem userBagItem = userBagItemMap.get(cardId);
                    GoodsInfo goodsInfo = GoodsMgr.getGoodsById(cardId);
                    if (goodsInfo != null && userBagItem != null && userBagItem.getNum() > 0) {
                        cardCount += userBagItem.getNum();
                        removeProp.addProperty(cardId, BigInteger.valueOf(userBagItem.getNum()));
                        getProp.addProperty(eItemId.DFW_GOLDEN.getValue(), goodsInfo.getParamList().get(1).multiply(BigInteger.valueOf(userBagItem.getNum())));
                    }
                }

                // 扣道具
                player.getModule(CurrencyModule.class).removeCurrency(removeProp, eLogMoneyType.DaFuWeng, eLogMoneyType.DaFuWengEmptyItem);

                // 获取下银币的名称
                GoodsInfo scoreItemInfo = GoodsMgr.getGoodsById(eItemId.DFW_GOLDEN.getValue());
                if (scoreItemInfo != null) {
                    scoreItemName = ServerLanguageMgr.getContent(scoreItemInfo.getGoodsName(), player.getLanguage());
                }

                // 发奖励
                if (energyItemCount > 0 || cardCount > 0) {
                    String title = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.MONOPOLY_GOODS_AUTO_CONSUME_TITLE, player.getLanguage()), activityName);
                    String content = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.MONOPOLY_GOODS_AUTO_CONSUME_CONTENT, player.getLanguage()),
                            activityName, energyItemName, energyItemCount, cardCount, scoreItemName);
                    MailManager.sendMail(player.getUserId(), eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(getProp), content, title);
                }
            }
        }
    }


    /**
     * 清空敦煌石窟颜料
     */
    public void clearDunHuangShiKuItem(){
        if(DunHuangShiKuMgr.isActivityInClearTime()){
            // 通关文书 清空
            for(Integer propId : DunHuangShiKuMgr.consumeIdSet){
                clearBagItem(propId,eLogMoneyType.DunHuangShiKuConsumeItemRemoveCost);
            }
            // 颜料 邮件回收
            Property property = new Property();
            for(Integer propId : DunHuangShiKuMgr.coloringIdList){
                UserBagItem userBagItem = userBagItemMap.get(propId);
                if (userBagItem != null && userBagItem.getNum() > 0) {
                    for (int i = 0; i < userBagItem.getNum(); i++) {
                        OpenGoodsBagResult openGoodsBagResult = GoodsBagMgr.getRandomGoods(userBagItem.getGoodsId());
                        property.addProperty(openGoodsBagResult.getReward());
                    }
                    player.getModule(CurrencyModule.class).removeCurrency(new Property(userBagItem.getGoodsId(), BigInteger.valueOf(userBagItem.getNum())), eLogMoneyType.DunHuangShiKu, eLogMoneyType.DunHuangShiKuItemRemoveCost);
                }
            }
            if(property.getGoods().size() > 0){

                MailManager.sendMail(player.getUserId(), eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(property),MultipleLanguageMgr.getContent(MailManager.DUNHUANGSHIKU_RETURN_COLORING_ITEM_CONTENT ,player.getLanguage()),
                        MultipleLanguageMgr.getContent(MailManager.DUNHUANGSHIKU_RETURN_COLORING_ITEM_TITLE,player.getLanguage()));
            }
        }
    }


    /**
     * 清空水浒叶子牌道具
     */
    private void clearShuiHuYeZiCardItem(){
        if(ShuiHuYeZiCardMgr.getConfig() == null || ShuiHuYeZiCardMgr.getActivityInfo() == null || ActivityMgr.activityOverTime(ShuiHuYeZiCardMgr.getActivityInfo())){
            // 活动结束后 清空 送福叶子 斗牌请帖 金叶子
            clearBagItem(eItemId.SHUIHUYEZICARD_DISPERSE_GOLD_ITEM.getValue(), eLogMoneyType.ShuiHuYeZiCardGoodsRemove);

        }
    }

    /**
     * 清空周年庆活动道具
     */
    private void clearCelebrationItem(){
        if (ActivityMgr.activityInShowPeriod(CelebrationMgr.getActivityInfo()) || ActivityMgr.activityInRewardPeriod(CelebrationMgr.getActivityInfo())) {
            for(int propId : CelebrationMgr.getCelebrationActivityConfig().getPropList()){
                removeCommonActivityBagItem(CelebrationMgr.getActivityInfo(),propId);
            }
        }
    }

    public List<Integer> getGoodsIdList() {
        List<Integer> list = new ArrayList<>();
        for (UserBagItem item : userBagItemMap.values()) {
            if (item.getNum() > 0) {
                list.add(item.getGoodsId());
            }
        }
        return list;
    }


    //移除小当家2挑菜牌
    public void clearCookBoy2PickUpItem(ActivityInfo activityInfo){
        int pickUpItemId = CookBoyActivityMgr.getCookBoy2PickUpItemId(activityInfo.getActivityId());
        int integralItemId = CookBoyActivityMgr.getCookBoy2IntegralItemId(activityInfo.getActivityId());
        UserBagItem userBagPickUpItem = player.getModule(BagModule.class).getUserBagItem(pickUpItemId);
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(pickUpItemId);
        if(userBagPickUpItem != null && userBagPickUpItem.getNum() > 0){
            long removeNum = userBagPickUpItem.getNum();
            String integralNum = BigInteger.valueOf(userBagPickUpItem.getNum()).multiply(goodsInfo.getParamList().get(1)).toString();
            String reward = integralItemId + "=" + integralNum;
            String mailTitle = MultipleLanguageMgr.getContent(MailManager.COOK_BOY2_ITEM_RECOVERY_TITLE, player.getUserInfo().getLanguage());
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COOK_BOY2_ITEM_RECOVERY_CONTENT, player.getUserInfo().getLanguage()),
                    removeNum,integralNum);
            MailManager.sendMail(player.getUserId(), eMailType.SYSTEM.getValue(), reward, mailContent, mailTitle);
            player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(pickUpItemId + "=" + userBagPickUpItem.getNum()), eLogMoneyType.CookBoy,eLogMoneyType.CrossCookBoy2ActivityNewAc);
        }
    }

    public void clearCookBoyHpItem(ActivityInfo activityInfo){
        int hpItemId = CookBoyActivityMgr.getCookBoyHpItemId(activityInfo.getActivityId());
        int integralItemId = CookBoyActivityMgr.getCookBoyIntegralItemId(activityInfo.getActivityId());
        UserBagItem userBagHpItem = player.getModule(BagModule.class).getUserBagItem(hpItemId);
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(hpItemId);
        if(userBagHpItem != null && userBagHpItem.getNum() > 0){
            long removeNum = userBagHpItem.getNum();
            String integralNum = BigInteger.valueOf(userBagHpItem.getNum()).multiply(goodsInfo.getParamList().get(1)).toString();
            String reward = integralItemId + "=" + integralNum;
            String mailTitle = MultipleLanguageMgr.getContent(MailManager.COOKBOY_RETURN_ITEM_TITLE, player.getUserInfo().getLanguage());
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COOKBOY_RETURN_ITEM_CONTENT, player.getUserInfo().getLanguage()),
                    removeNum,integralNum);
            MailManager.sendMail(player.getUserId(), eMailType.SYSTEM.getValue(), reward, mailContent, mailTitle);
            player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(hpItemId + "=" + userBagHpItem.getNum()), eLogMoneyType.CookBoy,eLogMoneyType.CrossCookBoyActivityNewAc);
        }
    }

    /**
     * 回收道具
     *
     * @param itemId             回收的道具id
     * @param reward             每个道具转换对应的道具
     * @param recycleMailTitle   邮件标题
     * @param recycleMailContent 邮件内容
     */
    private void recycleItem(int itemId, Property reward, String recycleMailTitle, String recycleMailContent) {
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(itemId);
        if (goodsInfo == null) {
            return;
        }
        UserBagItem userBagItem = userBagItemMap.get(itemId);
        if (userBagItem == null) {
            return;
        }
        long num = userBagItem.getNum();
        if (num <= 0) {
            return;
        }
        // 移除道具
        player.getModule(CurrencyModule.class).removeCurrency(new Property(userBagItem.getGoodsId(), BigInteger.valueOf(num)), eLogMoneyType.System, eLogMoneyType.SystemRecycle);
        // 转化道具
        if (reward != null && !reward.isNothing()) {
            reward.goodCountMultiNum(num);
        }
        String mailTitle = MultipleLanguageMgr.getContent(recycleMailTitle, player.getLanguage());
        String goodsName = MultipleLanguageMgr.getContent(goodsInfo.getGoodsName(), player.getLanguage());
        String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(recycleMailContent, player.getLanguage()), goodsName, num);
        MailManager.sendMail(player.getUserId(), eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), mailContent, mailTitle);
    }

    /**
     * 回收道具
     *
     * @param itemId             回收的道具id
     */
    public void recycleItem(int itemId) {
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(itemId);
        if (goodsInfo == null) {
            return;
        }
        UserBagItem userBagItem = userBagItemMap.get(itemId);
        if (userBagItem == null) {
            return;
        }
        long num = userBagItem.getNum();
        if (num <= 0) {
            return;
        }
        // 移除道具
        player.getModule(CurrencyModule.class).removeCurrency(new Property(userBagItem.getGoodsId(), BigInteger.valueOf(num)), eLogMoneyType.System, eLogMoneyType.SystemRecycle);
    }



    /**
     * 回收联动材料
     */
    private void clearLinkageDishes(int linkageSwitch, int dishesId, String removeMailTitle, String removeMailContent) {
        if (0 == linkageSwitch) {//联动开关关闭
            DishesInfo dishes = DishesMgr.getDishes(dishesId);     //联动菜品
            if (null != dishes) {
                String[] split = dishes.getConsume().split(";");
                List<Integer> goodIds = Arrays.stream(split).map(s -> Integer.parseInt(s.split("=")[0])).collect(Collectors.toList());
                Property reward = new Property();
                Property removeItem = new Property();
                synchronized (userBagItemMap) {
                    for (Integer itemId : goodIds) {
                        UserBagItem userBagItem = userBagItemMap.get(itemId);
                        if (userBagItem != null && userBagItem.getNum() > 0) {
                            Property item = new Property(userBagItem.getGoodsId(), BigInteger.valueOf(userBagItem.getNum()));
                            removeItem.addProperty(item);
                            reward.addProperty(new Property(eItemId.INN_GAME_MONEY_INN.getValue(), BigInteger.valueOf(dishes.getCookSecond() * dishes.getMoneyPerSnd() * userBagItem.getNum() / 2)));
                            player.getModule(CurrencyModule.class).removeCurrency(item ,eLogMoneyType.Inn, eLogMoneyType.InnClearItem);
                        }
                    }
                }
                if (!reward.isNothing()) {//有将道具转换
                    String mailTitle = MultipleLanguageMgr.getContent(removeMailTitle, player.getLanguage());
                    String mailContent =MultipleLanguageMgr.getContent(removeMailContent, player.getLanguage());
                    MailManager.sendMailOnLoad(player, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), mailContent, mailTitle);
                }
            }
        }
    }

    public Map<Integer, UserBagItem> getUserBagItemMap() {
        return new HashMap<>(userBagItemMap);
    }
}
